using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using GameStateManagement;
using Microsoft.Devices.Sensors;


namespace UpChickenFar
{

    public class GamePlay : GameScreen
    {
        const float TOP_SPEED = 30;
        SpriteBatch _spriteBatch;
        Texture2D _menu_back;

        Cloud _cloud1;
        Cloud _cloud2;
        Cloud _cloud3;

        Chicken _chicken;

        Colum _col1;
        Colum _col2;

        Rotor _rotor1;
        Rotor _rotor2;

        Random _random;


        Vector2 _distance_brick = new Vector2(20, 80);

        Brick[,] _brick;
        Brick _brick_tmp;


        ContentManager _content;
        bool _allow_handle_input = true;

        Accelerometer accelSensor;
        //Vector3 accelReading;

        bool accelActive = false;

        public GamePlay()
        {
            _spriteBatch = UpChickenFar.spriteBatch;
            EnabledGestures = Microsoft.Xna.Framework.Input.Touch.GestureType.Tap;
        }

        public override void Activate(bool instancePreserved)
        {
            if (_content == null)
                _content = new ContentManager(ScreenManager.Game.Services, "Content");

            _menu_back = _content.Load<Texture2D>("Textures/Backgrounds/menuBackground");
            Texture2D _cloud1_texture = _content.Load<Texture2D>("Textures/Items/cloud1");
            Texture2D _cloud2_texture = _content.Load<Texture2D>("Textures/Items/cloud2");
            Texture2D _cloud3_texture = _content.Load<Texture2D>("Textures/Items/cloud3");

            _cloud1 = new Cloud(ScreenManager.Game, new Vector2(291, 50), _cloud1_texture, 0);
            _cloud2 = new Cloud(ScreenManager.Game, new Vector2(100, 217), _cloud2_texture, 0);
            _cloud3 = new Cloud(ScreenManager.Game, new Vector2(200, 387), _cloud3_texture, 0);

            Texture2D _col1_texture = _content.Load<Texture2D>("Textures/Items/col1");
            Texture2D _col2_texture = _content.Load<Texture2D>("Textures/Items/col2");

            _col1 = new Colum(ScreenManager.Game, new Vector2(66, 700), _col1_texture);
            _col2 = new Colum(ScreenManager.Game, new Vector2(107, 697), _col2_texture);

            Texture2D _rotor_texture = _content.Load<Texture2D>("Textures/Items/rotor");

            _rotor1 = new Rotor(ScreenManager.Game, new Vector2(81, 708), _rotor_texture, 1.1f, 0.15f);
            _rotor2 = new Rotor(ScreenManager.Game, new Vector2(120, 703), _rotor_texture, 0.9f, 0.2f);

            Texture2D _chicken_texture = _content.Load<Texture2D>("Textures/Items/1");

            _chicken = new Chicken(ScreenManager.Game, new Vector2((ScreenManager.GraphicsDevice.Viewport.Width - _chicken_texture.Width)/2, ScreenManager.GraphicsDevice.Viewport.Height - _chicken_texture.Height), _chicken_texture);

            accelSensor = new Accelerometer();
            accelSensor.ReadingChanged +=
                new EventHandler<AccelerometerReadingEventArgs>(AccelerometerReadingChanged);


            try
            {
                accelSensor.Start();
                accelActive = true;
            }
            catch (AccelerometerFailedException e)
            {
                // the accelerometer couldn't be started.  No fun!
                accelActive = false;
            }
            catch (UnauthorizedAccessException e)
            {
                // This exception is thrown in the emulator-which doesn't support an accelerometer.
                accelActive = false;
            }

            _brick = new Brick[4, 10];
            _random = new Random();

            Texture2D _brick_texture = _content.Load<Texture2D>("Textures/Items/brick");
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    _brick[i, j] = new Brick(ScreenManager.Game, new Vector2((_distance_brick.X + _brick_texture.Width) * i, (_distance_brick.Y + _brick_texture.Height) * j), _brick_texture,0 ,_random.Next(1,5));
                }
            }
            _brick_tmp = new Brick(ScreenManager.Game, new Vector2((_distance_brick.X + _brick_texture.Width) * 1, (_distance_brick.Y + _brick_texture.Height) * 5), _brick_texture, 0, 1);

        }

        public void AccelerometerReadingChanged(object sender, AccelerometerReadingEventArgs e)
        {
            if (accelActive)
            {
                _chicken.Set_X_Speed((float)e.X * 30);
                _cloud1.SetSpeed((float)e.X * 5);
                _cloud2.SetSpeed((float)e.X * 6);
                _cloud3.SetSpeed((float)e.X * 7);
            }
        }
        public override void HandleInput(GameTime gameTime, InputState input)
        {

            if (_allow_handle_input)
            {
                _rotor1.HandleInput(gameTime, input);
                _rotor2.HandleInput(gameTime, input);

            }
            _chicken.HandleInput(gameTime, input);
            base.HandleInput(gameTime, input);
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {

            _rotor1.Update(gameTime);
            _rotor2.Update(gameTime);
            _cloud1.Update(gameTime);
            _cloud2.Update(gameTime);
            _cloud3.Update(gameTime);
            _brick_tmp.Update(gameTime);
            
            if (_chicken.GetPosition().Y >= 800 - _chicken.GetChickenTexture().Height)
            {
                if (_chicken.GetChickenSpeed().Y > 0)
                {
                    _chicken.Set_Y_Speed(-TOP_SPEED);
                    _chicken.Set_Y_Speed(_chicken.GetChickenSpeed().Y + 1);
                }
                else
                {
                    _chicken.Set_Y_Speed(_chicken.GetChickenSpeed().Y + 1);
                }
            }
            else
            {
                //_chicken.Set_Y_Speed(_chicken.GetChickenSpeed().Y + 1);

                if ((_chicken.GetPosition().Y + _chicken.GetChickenTexture().Height >= _brick_tmp.GetPosition().Y && _chicken.GetPosition().Y + _chicken.GetChickenTexture().Height <= _brick_tmp.GetPosition().Y + _brick_tmp.GetBrickHeigh()) && _brick_tmp.GetDrawAble() == 1 && (_chicken.GetPosition().X + (_chicken.GetChickenTexture().Width / 3)*2 >= _brick_tmp.GetPosition().X && _chicken.GetPosition().X + _chicken.GetChickenTexture().Width / 2 <= (_brick_tmp.GetPosition().X + _brick_tmp.GetBrickWidth())) && _chicken.GetChickenSpeed().Y > 0)
                {
                    _chicken.Set_Y_Position(_brick_tmp.GetPosition().Y - _chicken.GetChickenTexture().Height);
                    _chicken.Set_Y_Speed(-TOP_SPEED);
                    _chicken.Set_Y_Speed(_chicken.GetChickenSpeed().Y + 1);
                }
                else
                {
                    _chicken.Set_Y_Speed(_chicken.GetChickenSpeed().Y + 1);
                }
                //for (int i = 0; i < 4; i++)
                //{
                //    bool tt = false;
                //    for (int j = 0; j < 10; j++)
                //    {
                //        _brick[i, j].Update(gameTime);
                //        if ((_chicken.GetPosition().Y + _chicken.GetChickenTexture().Height >= _brick[i, j].GetPosition().Y && _chicken.GetPosition().Y + _chicken.GetChickenTexture().Height <= _brick[i, j].GetPosition().Y + _brick[i, j].GetBrickHeigh()) && _brick[i, j].GetDrawAble() == 1 && (_chicken.GetPosition().X + _chicken.GetChickenTexture().Width / 2 >= _brick[i, j].GetPosition().X && _chicken.GetPosition().X + _chicken.GetChickenTexture().Width / 2 <= (_brick[i, j].GetPosition().X + _brick[i, j].GetBrickWidth())) && _chicken.GetChickenSpeed().Y > 0)
                //        {
                //            _chicken.Set_Y_Position(_brick_tmp.GetPosition().Y - _chicken.GetChickenTexture().Height);
                //            _chicken.Set_Y_Speed(-TOP_SPEED);
                //            _chicken.Set_Y_Speed(_chicken.GetChickenSpeed().Y + 1);
                //            tt = true;
                //        }
                //        else
                //        {
                //            _chicken.Set_Y_Speed(_chicken.GetChickenSpeed().Y + 1);
                //        }
                //    }
                //    if (tt)
                //        break;
                //}

            }
            _chicken.Update(gameTime);
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }

        public override void Draw(GameTime gameTime)
        {
            _spriteBatch.Begin();
            _spriteBatch.Draw(_menu_back, Vector2.Zero, null, Color.White);
            _spriteBatch.End();
            _cloud1.Draw(gameTime);
            _cloud2.Draw(gameTime);
            _cloud3.Draw(gameTime);
            _col2.Draw(gameTime);
            _rotor2.Draw(gameTime);
            _col1.Draw(gameTime);
            _rotor1.Draw(gameTime);
            //for (int i = 0; i < 4; i++)
            //{
            //    for (int j = 0; j < 10; j++)
            //    {
            //        _brick[i, j].Draw(gameTime);
            //    }
            //}
            _brick_tmp.Draw(gameTime);
            _chicken.Draw(gameTime);
            base.Draw(gameTime);
        }


    }
}
